package com.example.woke1;

import java.util.ArrayList;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.http.HttpMethods;
import com.zhongcai.online.R;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;
import com.woke.data.Data_zhcaipeople;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.app.Activity;

public class DingqipeopleActivity extends Activity {

	private ListView mListView;
	private String id;
	private ArrayList<Data_zhcaipeople> datalist = new ArrayList<Data_zhcaipeople>();
	private Myadapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_dingqipeople);
		
		id = getIntent().getStringExtra("id");
		
		findViewById(R.id.avdingqipeople_image_back).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		mListView = (ListView) findViewById(R.id.avdingqipeople_listview);
		adapter = new Myadapter();
		mListView.setAdapter(adapter);
		getdingqipeople();
	}
	
	private void getdingqipeople() {
		AsyncHttpClient ahc = new AsyncHttpClient();
        PersistentCookieStore myCookieStore = new PersistentCookieStore(DingqipeopleActivity.this);    
        ahc.setCookieStore(myCookieStore); 
		RequestParams params = new RequestParams();
		params.put("g", "api");
		params.put("m", "financial");
		params.put("a", "dingqiRecord");
		params.put("finanid", id);
		Log.e("params", params.toString());
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				Log.e("response", "response"+response);
				try {
					String flag = response.getString("flag");
					if(flag.equals("success")){
						JSONObject object = response.getJSONObject("data");
						JSONArray jsonArray = object.getJSONArray("data");
						for (int i = 0; i < jsonArray.length(); i++) {
							JSONObject jsonObject = jsonArray.getJSONObject(i);
							String createdate = jsonObject.getString("createdate");
							String money = jsonObject.getString("money");
							String userid = jsonObject.getString("userid");
							String id = jsonObject.getString("id");
							String phone = jsonObject.getString("phone");
							String count = jsonObject.getString("count");
							Data_zhcaipeople list = new Data_zhcaipeople(createdate, money, userid, id, phone, count);
							datalist.add(list);
						}
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
				Log.e("response", "response"+datalist.size());
				if(datalist.size() > 0 ){
					adapter.notifyDataSetChanged();
				}
				
			}
			
			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
//				Log.e("�ϴ�", "�ֻ��ʧ��"+responseString);
			}
			
			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
//				mProgress.setVisibility(View.VISIBLE);
			}
			
			@Override
			public void onFinish() {
				super.onFinish();
//				mProgress.setVisibility(View.GONE);
			}
		});
	}

	class Myadapter extends BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return datalist.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}
		
		class Holder {
//			ImageView mIicon;
			TextView mTbankname;
			TextView mTbankcarno;
			TextView mTcardtype;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			Holder holder = null;
			if(convertView == null){
				holder = new Holder();
				convertView = getLayoutInflater().inflate(R.layout.item_adter_people,null);
				holder.mTbankname = (TextView) convertView.findViewById(R.id.item_adapter_peopledate);
				holder.mTbankcarno = (TextView) convertView.findViewById(R.id.item_adapter_peoplemoney);
				holder.mTcardtype = (TextView) convertView.findViewById(R.id.item_adapter_peoplephne);
				convertView.setTag(holder);
			}else{
				 holder = (Holder) convertView.getTag();
			}
			
			Data_zhcaipeople data = datalist.get(position);
			holder.mTbankname.setText(data.createdate);
			holder.mTbankcarno.setText(data.money+"元");
			holder.mTcardtype.setText(data.phone);
			return convertView;
		}
		
	}
	
}
